package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.common.result.PageResult;
import com.crm.domain.Course;
import com.crm.domain.SysOperLog;
import com.crm.dto.OperLogDto;
import com.crm.mapper.SysOperLogMapper;
import com.crm.service.OperLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class OperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements OperLogService {
    @Autowired
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     */
    @Override
    public void insertOperlog(SysOperLog operLog) {
        operLogMapper.insert(operLog);
    }



    @Override
    public void cleanOperLog() {
        log.info("清空操作日志");
        operLogMapper.cleanOperLog();
    }

    /**
     * 分页查询操作日志
     */
    @Override
    public PageResult PageList(OperLogDto operLogDto) {
        log.info("分页查询操作日志");
        Page<SysOperLog> page = new Page<>(operLogDto.getPageNum(), operLogDto.getPageSize());
        LambdaQueryWrapper<SysOperLog> queryWrapper = new LambdaQueryWrapper<>();
        // operLogDto的keyword属性和SysOperLog所有不是时间的属性都可以模糊匹配
        queryWrapper.like(operLogDto.getKeyword() != null, SysOperLog::getTitle, operLogDto.getKeyword())
                .like(operLogDto.getKeyword() != null, SysOperLog::getBusinessType, operLogDto.getKeyword())
                .like(operLogDto.getKeyword() != null, SysOperLog::getOperName, operLogDto.getKeyword())
                .like(operLogDto.getKeyword() != null, SysOperLog::getOperIp, operLogDto.getKeyword())
                .like(operLogDto.getKeyword() != null, SysOperLog::getOperLocation, operLogDto.getKeyword())
                .like(operLogDto.getKeyword() != null, SysOperLog::getDeptName, operLogDto.getKeyword())
                .like(operLogDto.getKeyword() != null, SysOperLog::getRequestMethod, operLogDto.getKeyword())
                .like(operLogDto.getKeyword() != null, SysOperLog::getOperUrl, operLogDto.getKeyword());

        if (operLogDto.getParams().get("beginCreateTime")!= null&&operLogDto.getParams().get("beginCreateTime") != "" &&operLogDto.getParams().get("endCreateTime") != null && operLogDto.getParams().get("endCreateTime") != ""){
            queryWrapper.between(SysOperLog::getCreateTime, operLogDto.getParams().get("beginCreateTime"), operLogDto.getParams().get("endCreateTime"))
                    .orderByDesc(SysOperLog::getCreateTime);
        }
        page(page, queryWrapper);
        return new PageResult(page.getTotal(), page.getRecords());

    }

}
